package com.example.yls.demoa;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int GET_IMG = 1001;
    private static final int GET_PHONE = 1002;
    private Button btn1;
    private Button btn2;
    private ImageView img_view;
    private Handler handler;

    private TextView tv_result;
    private EditText et_phone;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn1 = findViewById(R.id.btn1);
        btn2 = findViewById(R.id.btn2);
        img_view = findViewById(R.id.img_view);
        tv_result = findViewById(R.id.tv_result);
        et_phone = findViewById(R.id.et_phone);


        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == GET_IMG) {
                    Bitmap bitmap = (Bitmap) message.obj;
                    img_view.setImageBitmap(bitmap);
                    return true;
                } else if (message.what == GET_PHONE) {
                    String phone = (String) message.obj;
                    tv_result.setText(phone);
                    return true;

                }
                return false;
            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone = et_phone.getText().toString();
                        usehttpUrlConnectionGet(phone);
                    }
                }).start();
            }
        });

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        String phone = et_phone.getText().toString();
                        usehttpUrlConnectionPost(phone);
                    }
                }).start();
            }
        });
    }

    private void usehttpUrlConnectionPost(String phone) {

        String baseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";


        try {
            URL url = new URL(baseUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);

            String body = "mobileCode=".concat(phone).concat("&userID=");
            OutputStream outputStream = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"utf-8"));
            bufferedWriter.write(body);
            bufferedWriter.flush();
            bufferedWriter.close();


            int code = httpURLConnection.getResponseCode();
            Log.e(TAG, "code = " + code);

            InputStream inputStream = httpURLConnection.getInputStream();
//            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//            Message message = new Message();
//            message.what = GET_IMG;
//            message.obj = bitmap;
//            handler.sendMessage(message);
//            img_view.setImageBitmap(bitmap);


            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e(TAG, "stringBuffer = " + stringBuffer.toString());


            Message message = Message.obtain();
            message.what = GET_PHONE;
            message.obj = stringBuffer.toString();
            handler.sendMessage(message);


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    private void usehttpUrlConnectionGet(String phone) {
        String strUrl = "http://www.baidu.com";
        String imgUrl = "http://uploads.5068.com/allimg/171120/1-1G1201A635.jpg";

        String baseUrl = "http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
        String phoneUrl = baseUrl.concat("?mobileCode=").concat(phone).concat("&userID=");

        try {
            URL url = new URL(phoneUrl);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            int code = httpURLConnection.getResponseCode();
            Log.e(TAG, "code = " + code);

            InputStream inputStream = httpURLConnection.getInputStream();
//            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//            Message message = new Message();
//            message.what = GET_IMG;
//            message.obj = bitmap;
//            handler.sendMessage(message);
//            img_view.setImageBitmap(bitmap);


            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            StringBuffer stringBuffer = new StringBuffer();
            String line = bufferedReader.readLine();
            while (line != null) {
                stringBuffer.append(line);
                line = bufferedReader.readLine();
            }
            Log.e(TAG, "stringBuffer = " + stringBuffer.toString());


            Message message = Message.obtain();
            message.what = GET_PHONE;
            message.obj = stringBuffer.toString();
            handler.sendMessage(message);


        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
